package org.huel.backend.controller;

import org.huel.backend.domain.entity.AbsencePrediction;
import org.huel.backend.service.impl.PredictionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/prediction")
public class PredictionController {

    @Autowired
    private PredictionService predictionService;

    /**
     * 获取学生的缺勤预测记录
     *
     * @param studentId 学生ID
     * @return 预测记录列表
     */
    @GetMapping("/student/{studentId}")
    public ResponseEntity<List<AbsencePrediction>> getStudentPredictions(
            @PathVariable String studentId) {
        List<AbsencePrediction> predictions = predictionService.getStudentAbsencePredictions(studentId);
        return ResponseEntity.ok(predictions);
    }

    /**
     * 获取高风险缺勤预测记录
     *
     * @param threshold 风险阈值 (0-1)
     * @return 高风险预测记录
     */
    @GetMapping("/high-risk")
    public ResponseEntity<List<AbsencePrediction>> getHighRiskPredictions(
            @RequestParam(defaultValue = "0.7") float threshold) {
        List<AbsencePrediction> predictions = predictionService.getHighRiskPredictions(threshold);
        return ResponseEntity.ok(predictions);
    }

    /**
     * 添加或更新缺勤预测记录
     *
     * @param prediction 预测记录
     * @return 操作结果
     */
    @PostMapping
    public ResponseEntity<Map<String, Object>> savePrediction(@RequestBody AbsencePrediction prediction) {
        boolean success = predictionService.saveAbsencePrediction(prediction);

        Map<String, Object> response = new HashMap<>();
        response.put("success", success);
        response.put("message", success ? "预测记录保存成功" : "预测记录保存失败");

        return ResponseEntity.ok(response);
    }

    /**
     * 获取风险最高的N个学生
     *
     * @param limit 数量限制
     * @return 预测记录列表
     */
    @GetMapping("/top-risk")
    public ResponseEntity<List<AbsencePrediction>> getTopRiskStudents(
            @RequestParam(defaultValue = "10") int limit) {
        List<AbsencePrediction> predictions = predictionService.getTopRiskStudents(limit);
        return ResponseEntity.ok(predictions);
    }
}